package com.makefuture.GeneratorModel;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.generator.ColumnMeta;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.activerecord.generator.TableMeta;

public class SqlserverMetaBuilder extends MetaBuilder {

    // 是否将不带前缀的表，加入的生成队列 默认true,就是所有表都会自动生成
    private boolean flag = true;
    //数据库用户名
    private String user = "";

    public SqlserverMetaBuilder(DataSource dataSource) {
        super(dataSource);
        // TODO Auto-generated constructor stub
    }

    public SqlserverMetaBuilder(DataSource dataSource, boolean flag,String user) {
        super(dataSource);
        this.flag = flag;
        this.user = user;
    }

    protected void buildTableNames(List<TableMeta> ret) throws SQLException {
        ResultSet rs = dbMeta.getTables(conn.getCatalog(), null, null,
                new String[] { "TABLE"});
        while (rs.next()) {
            String tableName = "";
            String tableSchem = rs.getString("TABLE_SCHEM");
            if (user.equalsIgnoreCase(tableSchem)) {
                tableName = rs.getString("TABLE_NAME");
            }

            if (excludedTables.contains(tableName)) {
                System.out.println("Skip excluded table :" + tableName);
            } else {
                TableMeta tableMeta = new TableMeta();
                tableMeta.name = tableName;
                tableMeta.remarks = rs.getString("REMARKS");

                boolean tempFlag = false;
                // 移除表名前缀仅用于生成 modelName、baseModelName。tableMeta.name 表名自身不受影响
                if (removedTableNamePrefixes != null) {
                    for (String prefix : removedTableNamePrefixes) {
                        if (tableName.startsWith(prefix)) {
                            tableName = tableName.replaceFirst(prefix, "");
                            tempFlag = true;
                            break;
                        }
                    }
                }
                if ((flag || tempFlag)&&!tableName.isEmpty()) {
                    tableMeta.modelName = StrKit.firstCharToUpperCase(StrKit
                            .toCamelCase(tableName));
                    tableMeta.baseModelName = "Base" + tableMeta.modelName;
                    ret.add(tableMeta);
                }
            }
        }
        rs.close();
    }
    
    
    protected void buildColumnMetas(TableMeta tableMeta) throws SQLException {
        String sql = dialect.forTableBuilderDoBuild(tableMeta.name);
        sql = sql.replaceAll("`", "");
        Statement stm = conn.createStatement();
        ResultSet rs = stm.executeQuery(sql);
        ResultSetMetaData rsmd = rs.getMetaData();
        
        for (int i=1; i<=rsmd.getColumnCount(); i++) {
            ColumnMeta cm = new ColumnMeta();
            cm.name = rsmd.getColumnName(i);
            
            String colClassName = rsmd.getColumnClassName(i);
            String typeStr = typeMapping.getType(colClassName);
            if (typeStr != null) {
                cm.javaType = typeStr;
            }
            else {
                int type = rsmd.getColumnType(i);
                if (type == Types.BINARY || type == Types.VARBINARY || type == Types.BLOB) {
                    cm.javaType = "byte[]";
                }
                else if (type == Types.CLOB || type == Types.NCLOB) {
                    cm.javaType = "java.lang.String";
                }
                else {
                    cm.javaType = "java.lang.String";
                }
            }
            
            // 构造字段对应的属性名 attrName
            cm.attrName = buildAttrName(cm.name);
            
            tableMeta.columnMetas.add(cm);
        }
        
        rs.close();
        stm.close();
    }
    public static void main(String[] args) {
		
	}
}